// walkman layout
// walkman.scene

var Walkman = cc.Layer.extend({

    _touchPos: null,
    _littlePerson: null,
    _redMan: null,

    init: function () {

        var ret = false;
        if (this._super()) {

            var winSize = cc.director.getWinSize();
            var centerPos = cc.p(winSize.width / 2, winSize.height / 2);

            //缓存动画图片
            cc.spriteFrameCache.addSpriteFrames(res.walkman_plist);

            //小人 精灵
            this._littlePerson = cc.Sprite.create(res.walkman_2);
            this._littlePerson.x = 100;
            this._littlePerson.y = 200;

            this.addChild(this._littlePerson);

            //小人的帧动画
            var littlePersonFrames = [];
            littlePersonFrames.push(cc.spriteFrameCache.getSpriteFrame("1.png"));
            littlePersonFrames.push(cc.spriteFrameCache.getSpriteFrame("2.png"));
            littlePersonFrames.push(cc.spriteFrameCache.getSpriteFrame("3.png"));

            //让小人动起来
            this._littlePerson.runAction(cc.RepeatForever.create(cc.Animate.create(cc.Animation.create(littlePersonFrames, 0.1))));

            //让小人移动
            //让他从做到右，然后从右到左移动
            var runWay = cc.Sequence.create(
                cc.MoveTo.create(5, cc.p(winSize.width, this._littlePerson.y)),
                cc.MoveTo.create(5, cc.p(this._littlePerson.x, this._littlePerson.y))
            );

            this._littlePerson.runAction(cc.RepeatForever.create(runWay));


            //放进小红人
            this._redMan = cc.Sprite.create(res.red_man_png);
            this._redMan.x = 200;
            this._redMan.y = winSize.height - 300;

            this.addChild(this._redMan);


            _instanceWalkman = this;

            cc.log("walkman 创建完毕。。。。");

            //监听触摸事件 {{
            if ('mouse' in cc.sys.capabilities)
                cc.eventManager.addListener({
                    event: cc.EventListener.MOUSE,
                    onMouseDown: function (event) {
                        event.getCurrentTarget().beginTouch();
                    },
                    onMouseUp: function (event) {
                        event.getCurrentTarget().resetTouch();
                    },
                    onMouseMove: function (event) {
                        if (event.getButton() != undefined)
                            event.getCurrentTarget().processEvent(event);
                    }
                }, this);
            if (cc.sys.capabilities.hasOwnProperty('touches')) {
                cc.eventManager.addListener({
                    event: cc.EventListener.TOUCH_ALL_AT_ONCE,
                    onTouchesBegan: function (touches, event) {
                        event.getCurrentTarget().beginTouch(touches[0]);
                    },
                    onTouchesEnded: function (touches, event) {
                        event.getCurrentTarget().resetTouch(touches[0]);
                    },
                    onTouchesMoved: function (touches, event) {
                        event.getCurrentTarget().processEvent(touches[0]);
                    }
                }, this);
            }
            // }} 监听触摸事件结束

            ret = true;
        }

        //创建一个定时器检查是否发生碰撞
        this.schedule(this.checkCollide, 0.1);

        return ret;

    },

    beginTouch: function (touch) {

    },
    resetTouch: function (touch) {
        this._touchPos = null;
    },
    processEvent: function (touch) {
        if (this._touchPos != null) {
            var target = this._littlePerson;
            var curPos = cc.p(target.x, target.y);
            var pos = touch.getLocation();
            curPos = cc.p(curPos.x + pos.x - this._touchPos.x, curPos.y + pos.y - this._touchPos.y);

            var winSize = cc.director.getWinSize();

            //x
            if (curPos.x >= 0 && curPos.x <= winSize.width) {
                target.x = curPos.x;
            } else if (curPos.x < 0) {
                target.x = 0;
            } else {
                target.x = winSize.width;
            }
            //y
            if (curPos.y >= 0 && curPos.y <= winSize.height) {
                target.y = curPos.y;
            } else if (curPos.y < 0) {
                target.y = 0;
            } else {
                target.y = winSize.height;
            }
        } else {
            //
        }
        this._touchPos = touch.getLocation();
    },

    //碰撞区域
    collideRect: function () {
        return cc.rect(this._littlePerson.x, this._littlePerson.y, 50, 100);
    },

    //检查是否发生碰撞
    checking: false,
    checkCollide: function () {
        if (this.checking) return;
        this.checking = true;

        //小红人的碰撞区域
        var redCollideRect = cc.rect(this._redMan.x, this._redMan.y, 150, 150);
        if (cc.rectIntersectsRect(this.collideRect(), redCollideRect)) {

            console.log("发生碰撞啦~~~~~~~");

            var opac1 = cc.FadeTo.create(0.2, 50); //透明
            var opac2 = cc.FadeTo.create(0.2, 255); //不透明
            this._redMan.runAction(cc.Sequence.create(opac1, opac2, opac1, opac2, opac1, opac2, opac1, opac2));
            this._redMan.runAction(cc.RotateBy.create(0.6, 360, 0));
            //this._redMan.runAction(cc.sequence(cc.rotateTo(1,180,0), cc.rotateTo(1, 360, 0)));
        }

        //具体检查
        this.checking = false;
    }

});

Walkman.create = function () {
    var wk = new Walkman();
    if (wk && wk.init()) {
        return wk;
    }
    return null;
};

Walkman.scene = function () {
    var scene = cc.Scene.create();
    var layer = Walkman.create();
    scene.addChild(layer);
    return scene;
};